//
//  FavouriteApi.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/14.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import Moya

enum FavouriteApi {
    case getFavouriteRestaurants(page: Int)
    case checkFavourite(id: Int)
    case setFavourite(id: Int)
    case deleteFavourite(id: Int)
}

extension FavouriteApi: TargetType {
    
    var baseURL: URL {
        return URL(string: serverBasePath)!
    }
    
    var path: String {
        switch self {
        case .getFavouriteRestaurants:
            return "/user/favourites"
        case .checkFavourite, .setFavourite, .deleteFavourite:
            return "/user/favourites/hotels"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .setFavourite:
            return .post
        case .deleteFavourite:
            return .delete
        case .getFavouriteRestaurants, .checkFavourite:
            return .get
        }
    }
    
    var parameters: [String: Any]? {
        switch self {
        case .checkFavourite(let id):
            return ["id": id]
        case .deleteFavourite(let id):
            return ["id": id]
        case .setFavourite(let id):
            return ["id": id]
        case .getFavouriteRestaurants(let page):
            return ["page": page]
        }
    }
    
    var task: Task {
        return .request
    }
    
    var sampleData: Data {
        return "{\"data\":{\"msg\":\"good\"},\"meta\":{\"status\":200,\"msg\":\"OK\"}}".data(using: String.Encoding.utf8)!
    }
    
    var parameterEncoding: ParameterEncoding {
        switch self {
        case .deleteFavourite, .setFavourite:
            return JSONEncoding.default
        case .checkFavourite:
            return URLEncoding.queryString
        case .getFavouriteRestaurants:
            return URLEncoding.default
        }
    }
}
